<?php
/* @var $this SiteController */
/* @var $model Image */
/* @var $topuser User[] */
/* @var $topcollection Collection[] */
$this->pageTitle = Yii::app()->name;
?>
<!-- Welcome -->
<div id="welcome">
    <div id="welcome_content">
        <div id="model"></div>
        <div id="welcome_text">
            <h1>FCHIC.VN - MẠNG XÃ HỘI THỜI TRANG SỐ 1 VIỆT NAM</h1>
            <h2>Hãy cùng tham gia với chúng tôi</h2>
            <h3>Thỏa sức thể hiện phong cách, cá tính của bạn. Kết bạn, giao lưu với các tín đồ thời trang trên khắp Việt Nam, thật hấp dẫn phải không?</h3>
            <a href="<?php echo Yii::app()->createUrl('image/upload'); ?>" title="Đăng ký thành viên" class="big_btn effect">Chia sẻ ảnh ngay bây giờ!</a>
        </div>
    </div>
</div>
<!-- / welcome -->
<div class="line_grey"></div>
<!-- main home -->
<div id="main">
    <div id="main_content">
        <!-- main top -->
        <div id="main_top">
            <div class="mt_col">
                <div class="mt_col_name"><span style="margin-bottom: -25px;">Ai "xì tai" nhất?</span></div>
                <?php
                if ($topuser && sizeof($topuser) > 0) {
                    $command = Yii::app()->db->createCommand()->select('SUM(like_cnt)as like_cnt')->from('tbl_image')->where('user_created=:id and status=:status', array(':id' => $topuser[0]->id, ':status' => Image::IMAGE_ACTIVE));
                    $like_cnt = $command->queryRow();
                    $topimage = Image::model()->find(array(
                        'condition' => 'user_created=' . $topuser[0]->id . ' and status=' . Image::IMAGE_ACTIVE,
                        "order" => "like_cnt DESC",
                        "limit" => 1,
                    ));
                    foreach ($topuser as $u) {
                        if ($u == $topuser[0]) {
                            $checkFollow = UserFollow::model()->find("followed = :followed and follower =:follower", array(
                                ':followed' => $topuser[0]->id,
                                'follower' => Yii::app()->user->id
                            ));
                            ?>
                            <div class="mt_col_thumb">                  
                                <?php if (!$checkFollow && Yii::app()->user->id != null && Yii::app()->user->id != $topuser[0]->id) {
                                    ?> 
                                    <a id="user_<?php echo $topuser[0]->id; ?>" onclick="return follow(<?php echo $topuser[0]->id; ?>);" class="mt_col_btn" href="#" title="Theo dõi">Theo dõi</a>
                                <?php } ?>
                                <a href="<?php echo Yii::app()->createUrl('user/view', array('username' => $topuser[0]->username)); ?>"><img class="mt_col_avatar" src="<?php echo Yii::app()->baseUrl . '/upload/user/' . $topuser[0]->id . '/avatar/' . $topuser[0]->avatar; ?>" border="0" /></a>
                                <a href="<?php echo Yii::app()->createUrl('image/view', array('id' => $topimage->id)); ?>" title="<?php if ($topimage) echo $topimage->title; ?>"><img height="170px" src="<?php echo Yii::app()->baseUrl . '/upload/user/' . $topuser[0]->id . '/images/' . $topimage->img_url; ?>" border="0" title="<?php if ($topimage) echo $topimage->title; ?>" alt="<?php if ($topimage) echo $topimage->title; ?>" /></a>
                            </div>
                            <p><a class="mt_col_title" href="<?php echo Yii::app()->createUrl('user/view', array('username' => $topuser[0]->username)); ?>" title="<?php if ($topimage) echo $topimage->title; ?>">Tuần này : <?php echo $topuser[0]->fullname; ?></a></p>
                            <p class="liked"><?php echo $like_cnt['like_cnt']; ?> liked</p>

                            <?php
                        }else {
                            if($u==$topuser[1]) echo '<div class="who_like">';
                            ?>
                                <a href="<?php echo Yii::app()->createUrl('user/view', array('username' => $u->username)); ?>"><div class="avatar_border"><img title="<?php echo $u->fullname; ?>" src="<?php echo Yii::app()->baseUrl . '/upload/user/' . $u->id . '/avatar/' . $u->avatar; ?>" border="0" /></div></a>
                                <?php
                            }
                        }
                        ?>
                        <a href="#" title="Xem thêm"><div class="avatar_border"><div class="w_l_more effect">More</div></div></a>
                    </div>
                    <?php
                }
                ?>

            </div>
            <div class="mt_col">
                <div class="mt_col_name"><span style="margin-bottom: -25px;">Bộ sưu tập nổi bật</span></div>
                <?php
                if ($topcollection && sizeof($topcollection) > 0)
                    foreach ($topcollection as $c) {
                        $checkFollow = UserFollow::model()->find("followed = :followed and follower =:follower", array(
                            ':followed' => $c->user_created,
                            'follower' => Yii::app()->user->id
                        ));
                        if ($c == $topcollection[0]) {
                            $command = Yii::app()->db->createCommand()->select('count(collection_id)as size')->from('tbl_collection_image ci')->join('tbl_image i', 'ci.image_id=i.id')->where('ci.collection_id=:id and i.status=:status', array(':id' => $c->id, ':status' => Image::IMAGE_ACTIVE));
                            $size = $command->queryRow();
                            $image = Yii::app()->db->createCommand()->select('i.*')->from('tbl_image i')->join('tbl_collection_image ci', 'ci.image_id=i.id')->where('ci.collection_id=:id and i.status=:status', array(':id' => $c->id, ':status' => Image::IMAGE_ACTIVE))->limit('1')->order('(like_cnt * 5 + view_cnt) DESC')->queryRow();
                            ?>
                            <div class="mt_col_thumb">
                                <div class="mt_col_collect">
                                    <a href="<?php echo Yii::app()->createUrl('user/view', array('username' => $c->userCreated->username)); ?>" title="<?php echo $c->userCreated->fullname ?>"><img class="mt_col_avatar" src="<?php echo Yii::app()->baseUrl . '/upload/user/' . $c->user_created . '/avatar/' . $c->userCreated->avatar; ?>" border="0" title="<?php echo $c->userCreated->fullname ?>" /></a>
                                    <a href="<?php echo Yii::app()->createUrl('user/view', array('username' => $c->userCreated->username)); ?>" class="mt_col_username" title="<?php echo $c->userCreated->fullname ?>"><?php echo $c->userCreated->fullname ?></a>
                                    <?php if (!$checkFollow && Yii::app()->user->id != null && Yii::app()->user->id != $c->user_created) {
                                        ?>
                                        <a id="user_<?php echo $c->userCreated->id; ?>" onclick="return follow(<?php echo $c->userCreated->id; ?>);" class="mt_col_follow effect" title="Follow" href="#">Theo dõi</a>
                                    <?php } ?>
                                    <div class="mt_col_collect_view">
                                        <div class="mt_bst"><?php echo $size['size']; ?></div>
                                        <div class="mt_bst_view"><a href="<?php echo Yii::app()->createUrl('collection/view', array('id'=>$topcollection[0]->id)) ?>" title="Xem bộ sưu tập">Xem bộ sưu tập</a></div>
                                        <div class="clr"></div>
                                    </div>
                                </div>
                                <a href="<?php echo Yii::app()->createUrl('collection/view', array('id'=>$topcollection[0]->id)) ?>" title="<?php echo $c->name; ?>"><img  height="170px" src="<?php echo Yii::app()->baseUrl . '/upload/user/' . $image['user_created'] . '/images/' . $image['img_url']; ?>" border="0" title="<?php echo $c->name; ?>" alt="<?php echo $c->name; ?>" /></a>
                            </div>
                            <p><a class="mt_col_title" href="<?php echo Yii::app()->createUrl('collection/view', array('id'=>$topcollection[0]->id)) ?>" title="<?php echo $c->name; ?>"><?php echo $c->name; ?></a></p>
                            <p class="viewed"><?php echo $c->like_cnt; ?> liked</p>
                            <?php
                        } else {
                            ?>
                            <div class="home_chart">
                                <div class="hc_stt"><?php echo($c == $topcollection[1]) ? '2' : '3'; ?></div><div class="hc_title"><a href="<?php echo Yii::app()->createUrl('collection/view', array('id'=>$c->id)) ?>" title="<?php echo $c->name; ?>"><?php echo $c->name; ?></a></div>
                                <div class="clr"></div>
                                <div class="hc_pss"><div style="width: <?php echo($c == $topcollection[1]) ? '80%' : '60%'; ?>;" class="hc_pss_mask"><div class="hc_pss_loaded"></div></div></div>
                            </div>
                            <?php
                        }
                    }
                ?>
            </div>
            <div class="mt_col mt_col_end border">
                <div class="box_fb">
                    <div class="box_fb_tabs">
                        <ul class="idTabs">
                            <li><a href="#ct_box_fb" class="selected"><span class="share_icons share_icons_fb"></span></a></li>
                            <li><a href="#ct_box_tw"><span class="share_icons share_icons_tw"></span></a></li>
                            <li><a href="#ct_box_tr"><span class="share_icons share_icons_tr"></span></a></li>
                            <li><a href="#ct_box_pin"><span class="share_icons share_icons_pin"></span></a></li>
                        </ul>
                        <div class="clr"></div>
                        <div style="padding: 5px 5px 10px; overflow: hidden;">
                            <div id="ct_box_fb" style="display: none;">
                                <div class="fb-like-box" data-href="https://www.facebook.com/Fchic.vn" data-width="285" data-show-faces="true" data-header="false" data-stream="false" data-show-border="false"></div>
                                
                            </div>
                            <div id="ct_box_tw" style="display: none;">bbbbb</div>
                            <div id="ct_box_tr" style="display: none;">cccc</div>
                            <div id="ct_box_ig" style="display: none;">eeeee</div>
                            <div id="ct_box_pin" style="display: none;">fffff</div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="clr"></div>
        </div>
        <!-- / main top -->
        <div class="line_grey"></div>
        <!-- main grid -->
        <h1 class="mt10 stick">NỔI BẬT</h1>
        <div class="grid_mask mt10">
            <div class="grid">
                <section>
                    <ul class="items">
                        <?php
                        if ($model != null) {
                            foreach ($model as $i) {
                                $checkFollow = UserFollow::model()->find("followed = :followed and follower =:follower", array(
                                    ':followed' => $i->userCreated->id,
                                    'follower' => Yii::app()->user->id
                                ));
                                ?>
                                <li class="item">
                                    <div class="item_thumb">
                                        <div class="item_header">
                                            <img src="<?php echo Yii::app()->baseUrl . '/upload/user/' . $i->userCreated->id . '/avatar/' . $i->userCreated->avatar; ?>" border="0" class="ih_avatar" />
                                            <div class="ih_info">
                                                <a href="<?php echo Yii::app()->createUrl('user/view', array('username' => $i->userCreated->username)); ?>" class="ih_title" title="<?php echo $i->userCreated->fullname; ?>"><?php echo $i->userCreated->fullname; ?></a>
                                                <p><?php echo Format::timeAgo($i->created_at, 'Y-m-d H:i:s'); ?></p>
                                            </div>
                                            <?php if (!$checkFollow && Yii::app()->user->id != null && Yii::app()->user->id != $i->userCreated->id) {
                                                ?>                                
                                                <a id="user_<?php echo $i->userCreated->id; ?>" onclick="return follow(<?php echo $i->userCreated->id; ?>);" href="#" title="Follow" class="ih_follow effect">Theo dõi</a>
                                            <?php } ?>
                                        </div>
                                        <a href="#" title="Thích" class="pose_like"></a>
                                        <div class="item_bottom">
                                            <div class="ib_liked pose_icons"><?php echo $i->like_cnt ?></div>
                                            <div class="ib_comment pose_icons"><?php echo $i->view_cnt // echo sizeof(Comment::model()->findAll('image_id=:id', array(':id' => $i->id))); ?></div>
                                            <a href="#" title="Thêm vào bộ sưu tập" class="ib_save pose_icons"></a>
                                        </div>
                                        <a href="<?php echo Yii::app()->createUrl('image/view', array('id' => $i->id)); ?>"><img src="<?php echo Yii::app()->baseUrl . '/upload/user/' . $i->userCreated->id . '/images/' . $i->img_url; ?>" border="0" /></a>
                                    </div>
                                    <div class="item_info">
                                        <a href="<?php echo Yii::app()->createUrl('image/view', array('id' => $i->id)); ?>"><h1><?php echo $i->title; ?></h1></a>
                                    </div>
                                </li>
                                <?php
                                $rate = $i->like_cnt * 5 + $i->view_cnt;
                            }
                            if (sizeof($model) >= 12) {
                                ?>
                                <li class="item">
                                    <div class="item_more" id="<?php echo $rate; ?>">
                                        <a href="#" onclick="return loadmore();" title="Xem thêm"><span>+</span><div style="margin-top: -20px;">Xem thêm</div></a>
                                    </div>
                                </li>
                                <?php
                            }
                        } else {
                            echo "<h2><i>No results found.</i></h2>";
                        }
                        ?>
                    </ul>
                </section>
                <div class="clr"></div>
            </div>
        </div>
        <!-- / main grid -->
    </div>
</div>
<script type="text/javascript">
                                        function like(id) {
                                            $.ajax({
                                                url: "<?php echo Yii::app()->createUrl('image/like') . '?id='; ?>" + id,
                                                type: 'GET',
                                                success: function(data) {
                                                    var json = eval('(' + data + ')');
                                                    $("a.like_link_" + id).text(json['meg']);
                                                    $("p.like_cnt_" + id).text(json['like_cnt']);
                                                }
                                            });
                                            return false;
                                        }
                                        function follow(id) {
                                            $.ajax({
                                                url: "<?php echo Yii::app()->createUrl('user/follow') . '?id='; ?>" + id,
                                                type: 'GET',
                                                success: function() {
//                                                                $("#user_" + id).remove();
                                                    $('a[id*=user_' + id + ']').remove();
                                                }
                                            });
                                            return false;
                                        }
                                        function loadmore() {
                                            var rate = $(".item_more").attr('id');
                                            $.ajax({
                                                url: "<?php echo Yii::app()->createUrl('site/loadmore') . '?id='; ?>" + rate,
                                                type: 'GET',
                                                success: function(data) {
                                                    var json = eval('(' + data + ')');
                                                    for (var i = 0; i < json.length; i++) {
                                                        sdata = '';
                                                        sdata += '<li class="item" style="position: absolute; display: list-item; top: 0px; left: 735px;"><div class="item_thumb"><div class="item_header">';
                                                        sdata += '<img src="<?php echo Yii::app()->baseUrl . '/upload/user/'; ?>' + json[i].user_created + '/avatar/' + json[i].avatar + '" border="0" class="ih_avatar" />';
                                                        sdata += '<div class="ih_info">';
                                                        sdata += '<a href="' + json[i].user_url + '" class="ih_title" title="' + json[i].title + '">' + json[i].fullname + '</a>';
                                                        sdata += '<p>' + json[i].time + '</p></div>';
                                                        if (json[i].isFollowed) {
                                                            sdata += '<a id="user_' + json[i].user_created + '" onclick="return follow(' + json[i].user_created + ');" href="#" title="Follow" class="ih_follow effect">Theo dõi</a>';
                                                        }
                                                        sdata += '</div>';
                                                        sdata += '<a href="#" title="Thích" class="pose_like"></a>';
                                                        sdata += '<div class="item_bottom">';
                                                        sdata += '<div class="ib_liked pose_icons">' + json[i].like_cnt + '</div>';
                                                        sdata += '<div class="ib_comment pose_icons">' + json[i].comment_cnt + '</div>';
                                                        sdata += '<a href="#" title="Thêm vào bộ sưu tập" class="ib_save pose_icons"></a></div>';
                                                        sdata += '<a href="' + json[i].view_url + '"><img src="<?php echo Yii::app()->baseUrl . '/upload/user/'; ?>' + json[i].user_created + '/images/' + json[i].img_url + '" border="0" /></a>';
                                                        sdata += '</div><div class="item_info">';
                                                        sdata += '<a href="' + json[i].view_url + '"><h1>' + json[i].title + '</h1></a></div></li>';
//                                                                    $("li.item:last").after(sdata);
//                                                        $("ul.items").append(sdata);
                                                        $(sdata).hide().insertAfter("li.item:last").slideDown(400);
                                                    }
                                                }
                                            });
                                            return false;
                                        }
</script>
<div id="fb-root"></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/vi_VN/all.js#xfbml=1";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>

